package com.mijie.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * ApplicationProperties.INSTANCE.loadXML(event.getServletContext().getRealPath("") + "/WEB-INF/classes/properties.xml");
 *
 */
public enum ApplicationProperties {
	INSTANCE;
	private  Properties properties = new Properties();
	private  Logger logger = Logger.getLogger(ApplicationProperties.class);
	private  Map<String,Map<String,String>> xmlConf = new HashMap<String,Map<String,String>>();
	private RegionInfoFinder regionInfoFinder;
	
	
	private ApplicationProperties() {
	}

	public void load(File file) {
		FileInputStream fis = null;
		try {
			logger.info("load application properties file.");
			fis = new FileInputStream(file);
			INSTANCE.properties.load(fis);
		} catch (IOException e) {
			INSTANCE.logger.error(e.getMessage(), e);
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (Exception e) {
				}
			}
		}
	}

	protected  void loadXML(File file){
		InputStream is = null;
		try {
			is = new FileInputStream(file);
			DocumentBuilderFactory  factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc =  builder.parse(is);
			Element e = doc.getDocumentElement();
			NodeList groupNode = e.getChildNodes();
			Node group;
			int size = groupNode.getLength();
			for(int i=0;i<size;i++){
				group = groupNode.item(i);
				if(group.getNodeType()==Node.ELEMENT_NODE){
					logger.info("load xml conf -> "+group.getNodeName());
					xmlConf.put(group.getNodeName(), loadMap(doc,group.getNodeName()));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(is!=null)
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	
	protected  void loadRegionInfo(){
		 InputStream is = this.getClass().getClassLoader().getResourceAsStream("com/mijie/core/regionInfo.txt");
		 if(is==null){
			 logger.info("load regionInfo fail.can't found com/mijie/core/regionInfo.txt");
			 return;
		 }
		 try {
			regionInfoFinder = new RegionInfoFinder(is);
			logger.info("####load regionInfo,group size: "+regionInfoFinder.size()+",file:com/mijie/core/regionInfo.txt");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			regionInfoFinder = null;
		}
	}
	
	
	private static Map<String,String> loadMap(Document doc,String tagName) throws Exception{
		Map<String,String> map = new TreeMap<String,String>();
		NodeList list = doc.getElementsByTagName(tagName);
		Element e = (Element)list.item(0);
		list = e.getElementsByTagName("term");
		for(int i=0;i<list.getLength();i++){
			e = (Element)list.item(i);
			map.put(e.getAttribute("value"),e.getAttribute("name"));
		}
		return map;
	}
	
	public static RegionInfo findRegionInfo(double latitude,double longitude){
		return INSTANCE.regionInfoFinder.findRegionInfo(latitude, longitude);
	}
	
	public static Map<String,String> getXmlConf(String name){
		return INSTANCE.xmlConf.get(name);
	}
	
	public static String get(String sql) {
		return INSTANCE.properties.getProperty(sql);
	}
	
	public static Set<Object> getKeys(){
		return INSTANCE.properties.keySet();
	}
	
}
